package com.lmk.mkcc.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.lmk.mkcc.dto.PayextparamsDto;
import com.lmk.mkcc.enums.GlobalErrorCode;
import com.lmk.mkcc.service.ICourseUserLearnService;
import com.lmk.mkcc.util.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;


// 课程这边接收事务消息，将用户消息保存到中间表
@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "course-order-pay-group",
        topic = "pay-transaction-topic",
        selectorExpression = "pay-transaction-tag",
        messageModel = MessageModel.BROADCASTING
)
public class CoursePayAsyncNotifyTxConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseUserLearnService courseUserLearnService;
    @Override
    public void onMessage(MessageExt messageExt) {
        // 业务校验
        byte[] body = messageExt.getBody();
        String s = new String(body, StandardCharsets.UTF_8);
        // 将string字符串转化为对象
        PayextparamsDto payextparamsDto = JSON.parseObject(s, PayextparamsDto.class);
        AssertUtil.isNotNull(payextparamsDto, GlobalErrorCode.NO_DTO_ERROR);
        // 将信息保存到中间表
        courseUserLearnService.addPayextparamsDto(payextparamsDto);
    }
}
